package vanjalee.bs.com.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ToolTipManager;

public class MultiTable extends JPanel implements ActionListener {

	private static String help = "<html><head><title>HTML Generator Sample Page</title></head>"
			+ "<body><h2 style=\"text-align: center;\">Filter Instructions</h2><p dir=\"ltr\"><br />"
			+ "Use logical operations like:</p>"
			+ "<ul><li>&lt; [value]</li><li>&gt; [value]</li><li>= [value]</li><li>!&nbsp; [value]</li><li>&lt;= [value]</li><li>&gt;= [value]</li><li>&lt;&gt; [value]</li></ul>"
			+ "<p dir=\"ltr\" style=\"text-align: justify;\">"
			+ "These are best used with number values, but can also<br />"
			+ "be normally used with text information. If it comes to text<br />"
			+ "information filtering, use of the wirldcard, or \"*\" operator<br />"
			+ "is recommended. Wildcards represent the text following<br />"
			+ "or preceding the one we typed. If wildcard operators are<br />"
			+ "standing before the text, they stands for any text, ending<br />"
			+ "with our given text. Here are few examples:</p>"
			+ "<ul><li>*test &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; (search for rows that end with \"test\")</li>"
			+ "<li>*Special* (search for rows that contain \"Special\")</li>"
			+ "<li>Crafted* &nbsp; (search for rows that start with \"Crafted\")</li></ul><br /></body></html>";

	private static final String WEAPONS = "w";
	private static final String PARTS = "p";

	private static final String MAINC = "wc";
	private static final String LONGR = "wl";
	private static final String SUBG = "ws";
	private static final String MELEE = "wr";

	private static final String SAIL = "psa";
	private static final String STAB = "pst";
	private static final String ENG = "pen";
	private static final String FIGH = "pfg";
	private static final String HULL = "phl";

	private static final long serialVersionUID = 1L;
	private DataDisplay dataDisplay;
	private JPanel radioPanelWeapons;
	private JPanel radioPanelParts;
	private JPanel radioPanelWeaponsParts;
	private JRadioButton buttonMC;
	private JRadioButton buttonLR;
	private JRadioButton buttonSG;
	private JRadioButton buttonRM;
	private JRadioButton buttonSail;
	private JRadioButton buttonStab;
	private JRadioButton buttonEng;
	private JRadioButton buttonHull;
	private JRadioButton buttonFigh;
	private JRadioButton buttonWeapons;
	private JRadioButton buttonParts;
	private ButtonGroup groupParts;
	private ButtonGroup groupWeapons;
	private ButtonGroup groupMain;

	public MultiTable() {

		super(new BorderLayout());

		buttonMC = new JRadioButton("Main Cannon");
		buttonMC.setMnemonic(KeyEvent.VK_B);
		buttonMC.setActionCommand(MAINC);
		buttonMC.setSelected(true);

		buttonLR = new JRadioButton("Long Range");
		buttonLR.setMnemonic(KeyEvent.VK_C);
		buttonLR.setActionCommand(LONGR);

		buttonSG = new JRadioButton("Subgun");
		buttonSG.setMnemonic(KeyEvent.VK_D);
		buttonSG.setActionCommand(SUBG);

		buttonRM = new JRadioButton("Melee");
		buttonRM.setMnemonic(KeyEvent.VK_R);
		buttonRM.setActionCommand(MELEE);

		// Part radios

		buttonSail = new JRadioButton("Sails");
		buttonSail.setActionCommand(SAIL);

		buttonStab = new JRadioButton("Stabilizers");
		buttonStab.setActionCommand(STAB);

		buttonEng = new JRadioButton("Engine");
		buttonEng.setActionCommand(ENG);

		buttonHull = new JRadioButton("Hull");
		buttonHull.setActionCommand(HULL);

		buttonFigh = new JRadioButton("Figurehead");
		buttonFigh.setActionCommand(FIGH);

		// Main selection radios

		ToolTipManager.sharedInstance().setDismissDelay(20000);

		buttonWeapons = new JRadioButton("Weapons");
		buttonWeapons.setActionCommand(WEAPONS);
		buttonWeapons.setToolTipText(help);

		buttonParts = new JRadioButton("Parts");
		buttonParts.setActionCommand(PARTS);
		buttonParts.setToolTipText(help);

		groupWeapons = new ButtonGroup();
		groupWeapons.add(buttonMC);
		groupWeapons.add(buttonLR);
		groupWeapons.add(buttonSG);
		groupWeapons.add(buttonRM);

		groupMain = new ButtonGroup();
		groupMain.add(buttonWeapons);
		groupMain.add(buttonParts);

		groupParts = new ButtonGroup();
		groupParts.add(buttonEng);
		groupParts.add(buttonFigh);
		groupParts.add(buttonHull);
		groupParts.add(buttonSail);
		groupParts.add(buttonStab);

		// Register a listener for the radio buttons.
		buttonMC.addActionListener(this);
		buttonLR.addActionListener(this);
		buttonSG.addActionListener(this);
		buttonRM.addActionListener(this);
		buttonWeapons.addActionListener(this);
		buttonParts.addActionListener(this);
		buttonSail.addActionListener(this);
		buttonStab.addActionListener(this);
		buttonHull.addActionListener(this);
		buttonEng.addActionListener(this);
		buttonFigh.addActionListener(this);

		radioPanelWeaponsParts = new JPanel(new GridLayout(0, 1));
		radioPanelWeaponsParts.add(buttonWeapons);
		radioPanelWeaponsParts.add(buttonParts);

		// Put the radio buttons in a column in a panel.
		radioPanelWeapons = new JPanel(new GridLayout(0, 1));
		radioPanelWeapons.add(buttonMC);
		radioPanelWeapons.add(buttonSG);
		radioPanelWeapons.add(buttonRM);
		radioPanelWeapons.add(buttonLR);

		// Put the radio buttons in a column in a panel.
		radioPanelParts = new JPanel(new GridLayout(0, 1));
		radioPanelParts.add(buttonFigh);
		radioPanelParts.add(buttonHull);
		radioPanelParts.add(buttonStab);
		radioPanelParts.add(buttonEng);
		radioPanelParts.add(buttonSail);

		groupMain.setSelected(buttonWeapons.getModel(), true);

		this.add(radioPanelWeaponsParts, BorderLayout.LINE_START);
		setWeaponButtons();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		remove(dataDisplay);
		if (e.getActionCommand().equals(WEAPONS)) {
			setWeaponButtons();
			dataDisplay = new DataDisplay(4);
			groupWeapons.setSelected(buttonMC.getModel(), true);
		} else if (e.getActionCommand().equals(PARTS)) {
			setPartButtons();
			dataDisplay = new DataDisplay(5);
			groupWeapons.setSelected(buttonFigh.getModel(), true);
		}

		if (e.getActionCommand().equals(MELEE)) {
			dataDisplay = new DataDisplay(0);
		} else if (e.getActionCommand().equals(MAINC)) {
			dataDisplay = new DataDisplay(1);
		} else if (e.getActionCommand().equals(SUBG)) {
			dataDisplay = new DataDisplay(2);
		} else if (e.getActionCommand().equals(LONGR)) {
			dataDisplay = new DataDisplay(3);
		}

		if (e.getActionCommand().equals(ENG)) {
			dataDisplay = new DataDisplay(6);
		} else if (e.getActionCommand().equals(HULL)) {
			dataDisplay = new DataDisplay(7);
		} else if (e.getActionCommand().equals(FIGH)) {
			dataDisplay = new DataDisplay(8);
		} else if (e.getActionCommand().equals(STAB)) {
			dataDisplay = new DataDisplay(9);
		} else if (e.getActionCommand().equals(SAIL)) {
			dataDisplay = new DataDisplay(10);
		}

		add(dataDisplay, BorderLayout.CENTER);
		updateUI();
	}

	private void setWeaponButtons() {
		this.remove(radioPanelParts);
		this.add(radioPanelWeapons, BorderLayout.LINE_END);
		setBorder(BorderFactory.createEmptyBorder(20, 1, 20, 1));
		dataDisplay = new DataDisplay();
		add(dataDisplay, BorderLayout.CENTER);
	}

	private void setPartButtons() {
		this.remove(radioPanelWeapons);
		this.add(radioPanelParts, BorderLayout.LINE_END);
		setBorder(BorderFactory.createEmptyBorder(20, 1, 20, 1));
		dataDisplay = new DataDisplay();
		add(dataDisplay, BorderLayout.CENTER);
	}

}
